/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jlxy.ise.eroad.controller;

import com.google.gson.Gson;
import java.util.LinkedList;
import java.util.List;
import jlxy.ise.eroad.controller.Bean.*;
import jlxy.ise.eroad.service.CompanyService;
import jlxy.ise.eroad.service.Sret;
import jlxy.ise.eroad.service.Status;
import jlxy.ise.eroad.service.function.PackageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author hcc25_000
 */
@RestController
public class TranscationController {

    /* @Autowired
    private ClientService cs;
    @Autowired
    private DriverService ds;*/
    @Autowired
    private CompanyService coms;

    /*@Autowired
    private PlatformService ps;*/

    private String sret2json(Sret sr) {
        return new Gson().toJson(sr);
    }

    private String StatusJson(Status st) {
        return new Gson().toJson(st);
    }

    private String DetailOrderBeanJson(DetailOrderBean dob) {
        return new Gson().toJson(dob);
    }

    private String JOrderListBeanJson(JOrderListBean jlb) {
        return new Gson().toJson(jlb);
    }

    private String JJlistBeanJson(JJlistBean jlb) {
        return new Gson().toJson(jlb);
    }

    private String CdKeyBeanJson(CdKeyBean cb) {
        return new Gson().toJson(cb);
    }

    private String DetailSearchBeanJson(DetailSearchBean dsb) {
        return new Gson().toJson(dsb);
    }

    @RequestMapping("/api/regist")//注册
    public String Register(@RequestParam("username") String username, @RequestParam("passwd") String passwd, @RequestParam("cprovince") String cprovince, @RequestParam("ccity") String ccity, @RequestParam("cdistrict") String cdistrict, @RequestParam("caddr") String caddr) {
        Status st = coms.ComRegister(username, passwd, cprovince, ccity, cdistrict, caddr);
        return StatusJson(st);
    }

    @RequestMapping("/api/login")//登录
    public String Login(@RequestParam("username") String username, @RequestParam("passwd") String passwd) {
        Status st = coms.ComLogin(username, passwd);
        return StatusJson(st);
    }

    @RequestMapping("/api/allocate")//提交订单
    public String Allocate(@RequestParam("sprovince") String sprovince, @RequestParam("scity") String scity, @RequestParam("sdistrict") String sdistrict, @RequestParam("saddr") String saddr, @RequestParam("stime") String stime, @RequestParam("info") String info,
            @RequestParam("eprovince") String eprovince, @RequestParam("ecity") String ecity, @RequestParam("edistrict") String edistrict, @RequestParam("eaddr") String eaddr, @RequestParam("etime") String etime,
            @RequestParam("price") int price, @RequestParam("timelimit") int timelimit, @RequestParam("cname") String cname, @RequestParam("ctel") String ctel) {
        PackageBean pb = new PackageBean();
        pb.setSprovince(sprovince);
        pb.setScity(scity);
        pb.setSdistrict(sdistrict);
        pb.setSaddr(saddr);
        pb.setStime(stime);
        pb.setInfo(info);
        pb.setEprovince(eprovince);
        pb.setEcity(ecity);
        pb.setEdistrict(edistrict);
        pb.setEaddr(eaddr);
        pb.setPrice(price);
        pb.setTimelimit(timelimit);
        pb.setCname(cname);
        pb.setCtel(ctel);
        pb.setEtime(etime);
        Status st = coms.Allocate(pb);
        return new Gson().toJson(st);
    }

    @RequestMapping("/api/JJorderlist")//打开竞价列表
    public List JJorderlist() {
        return coms.getJOrderList();
    }

    @RequestMapping("/api/get-order-detail")//打开竞价列表后获取订单详情
    public String getOrderDetail(@RequestParam("id") int id) {
        return DetailOrderBeanJson(coms.getDetailedOrder(id));
    }

    @RequestMapping("/api/get-JJlist")//竞价列表
    public String getJJlist(@RequestParam("id") int id) {
        return JJlistBeanJson(coms.getJJlist(id));
    }

    @RequestMapping("/api/select-car")//选择车辆
    public String selectCar(@RequestParam("cid") int cid) {
        return CdKeyBeanJson(coms.SelectCar(cid));
    }

    @RequestMapping("/api/orderlist")//查看进行中的和已经完成的订单列表
    public List OrderList() {
        return coms.OrderList();
    }

    @RequestMapping("/api/cancelorder")//取消订单
    public String cancelOrder(@RequestParam("id") int id) {
        Status st = coms.CancelOrder(id);
        return StatusJson(st);
    }

    @RequestMapping("/api/confirmorder")//完成订单
    public String confirmOrder(@RequestParam("id") int id) {
        Status st = coms.ConfirmOrder(id);
        return StatusJson(st);
    }

    @RequestMapping("/api/ordersearch")//通过货物编号查询货物运输情况
    public String searchOrder(@RequestParam("oid") String oid) {
        DetailSearchBean dsb = coms.SearchOrder(oid);
        return DetailSearchBeanJson(dsb);
    }

    @RequestMapping("/api/test")
    public List lTest(@RequestParam("json") String json) {
        ClientBean cb = new Gson().fromJson(json, ClientBean.class);
        String username = cb.getUsername();
        String passwd = cb.getPasswd();
        username += "11";
        passwd += "11";
        List list = new LinkedList();
        list.add(username);
        list.add(passwd);
        return list;
    }
    /*@RequestMapping("/cc/random")
    public List cc_randrom(@RequestParam("username")String username,@RequestParam("passwd")String passwd){
        List list=new LinkedList();
        Map m=new HashMap<Object,String>();
        m.put("username",username);
        m.put("passwd",passwd);
        list.add(m);
        return list;
    }
    @RequestMapping("/com/register")//公司注册
    public String com_register(@RequestParam("value")String value){
        ComRegisterBean crb=new Gson().fromJson(value,ComRegisterBean.class);
        
        Sret sr=coms.regCompany(crb);
        return sret2json(sr);
    }
    
    @RequestMapping("/com/login")//公司登陆
    public String com_login(@RequestParam("value")String value){
        CompanyBean cb=new Gson().fromJson(value,CompanyBean.class);
        
        Sret sr=coms.logCompany(cb);
        return sret2json(sr);
    }
    
    @RequestMapping("/platform/allocate")//客户填写货物订单
    public String platform_allocate(@RequestParam("value")String value){
        Sret sr=null;
        UserOrderBean uob=new Gson().fromJson(value,UserOrderBean.class);
        sr=ps.setPackageInfo(uob);
        return sret2json(sr);
    }
   
    @RequestMapping("/platform/listAll")//公司全部订单信息
    public String platform_all(@RequestParam("value")String value){
        String username=value;
        Sret sr=coms.listAll(username);
        return sret2json(sr);
    }
    
    @RequestMapping("/platform/listWaiting")//等待运输的信息
    public String platform_waiting(@RequestParam("value")String value){
        WaitBean wb=new Gson().fromJson(value,WaitBean.class);
        Sret sr=coms.listWaiting(wb);
        return sret2json(sr);
    }
    
    @RequestMapping("/platform/listRunning")//运行中的货物信息
    public String platform_running(@RequestParam("value")String value){
        WaitBean wb=new Gson().fromJson(value,WaitBean.class);
        Sret sr=coms.listRunning(wb);
        return sret2json(sr);
    }
    
    @RequestMapping("/platform/listFinished")//已经完成运输的货物信息
    public String platform_finished(@RequestParam("value")String value){
        WaitBean wb=new Gson().fromJson(value,WaitBean.class);
        Sret sr=coms.listFinished(wb);
        return sret2json(sr);
    }
    
    @RequestMapping("/client/register")
    public String client_add(@RequestParam("people") String people) {//用户点击注册时触发
        //获取post中的people（json）的值
        ClientBean cb=new Gson().fromJson(people, ClientBean.class);
        String username=cb.getUsername();
        String passwd=cb.getPasswd();
        //同步到数据库
        cs.addClient(username, passwd);
        //返回username的值给html页面
        return username;
    }
    @RequestMapping("/client/login")
    public String client_search(@RequestParam("people") String people){//用户点击登录时触发
        String result=null;
        //获取bean
        ClientBean cb=new Gson().fromJson(people,ClientBean.class);
        String username=cb.getUsername();
        String passwd=cb.getPasswd();
        //查询数据库中的信息,成功返回welcome,失败返回error
        String res=cs.searchClient(username, passwd);
        
        if(res.equals("error")){
            result="failure";
        }else{
            result="username";
        }
        return result;
    }
    @RequestMapping("/client/order")
    public String client_key(@RequestParam("value")String order){//登录成功后,输入货物信息完毕后触发
        OrderBean ob=new Gson().fromJson(order, OrderBean.class);
        String result=cs.getCDKey(ob);
        return result;
    }
    
    @RequestMapping("/client/search")
    public String package_info(@RequestParam("cdkey")String cdkey){
        List list=cs.searchCDKey(cdkey);
        Map m=(HashMap)list.get(0);
       
        
        String res="";
        res="出发地:"+m.get("gaddress")+";"+"目的地:"+m.get("paddress");
        return res;
    }
    
    @RequestMapping("/com/search")
    public List pack_info(@RequestParam("value")String value){
        ComBean cb=new Gson().fromJson(value,ComBean.class);
        String phonenum=cb.getPhonenum();
        String state=cb.getState();
        List list=cs.getInfo(phonenum,state);
        if(list.isEmpty()){
            list.add("information error");
        }
        return list;
    }
    //用户提交货物信息
    @RequestMapping("/client/addOrder")
    public String addOrder(@RequestParam("value")String value){
        OrderBean ob=new Gson().fromJson(value, OrderBean.class);
        Sret result=cs.setPackageInfo(ob);
        return sret2json(result);
    }
    //公司获取货物信息
    @RequestMapping("/com/getOrder")
    public List getOrder(@RequestParam("value")String value){
        List list=cs.getPackageInfo(value);
        List newList=new LinkedList();
        for(int i=0;i<list.size();i++){
            Map m=(HashMap)list.get(i);
            String res=null;
            res="出发地:"+m.get("gaddress")+" "+"目的地:"+m.get("paddress")+" "+"订单运输号:"+m.get("cdkey")+" "+"订单时间："+m.get("time");
            newList.add(res);
        }
        return newList;
    }
   //司机竞价
    @RequestMapping("/driver/compete")
    public String competePrice(@RequestParam("value")String value){
        CompeteBean cb=new Gson().fromJson(value, CompeteBean.class);
        Sret sr=null;
        List list=ds.startCompete(cb);
        Map m=(Map)list.get(0);
        sr=coms.changeState((String)m.get("cdkey"));
        sr.setOk(""+m.get("price"));
       return sret2json(sr);
    } */
}
